"""Conversation memory management"""

from typing import List, Dict, Optional
from dataclasses import dataclass
from datetime import datetime

@dataclass
class ConversationExchange:
    """Single exchange in a conversation"""
    user_input: str
    assistant_response: str
    timestamp: datetime = datetime.now()

class ConversationMemory:
    """Manages conversation history"""
    
    def __init__(self, max_entries: int = 100):
        self.max_entries = max_entries
        self.exchanges: List[ConversationExchange] = []
    
    def add_exchange(self, user_input: str, assistant_response: str) -> None:
        """
        Add a new exchange to memory.
        
        Args:
            user_input (str): User's input
            assistant_response (str): Assistant's response
        """
        exchange = ConversationExchange(user_input, assistant_response)
        self.exchanges.append(exchange)
        
        # Maintain maximum size
        if len(self.exchanges) > self.max_entries:
            self.exchanges.pop(0)
    
    def get_context(self, num_exchanges: Optional[int] = None) -> str:
        """
        Get conversation context.
        
        Args:
            num_exchanges (Optional[int]): Number of recent exchanges to include
            
        Returns:
            str: Formatted conversation context
        """
        if num_exchanges is None:
            relevant_exchanges = self.exchanges
        else:
            relevant_exchanges = self.exchanges[-num_exchanges:]
            
        context = []
        for exchange in relevant_exchanges:
            context.extend([
                f"User: {exchange.user_input}",
                f"Assistant: {exchange.assistant_response}"
            ])
            
        return "\n".join(context)
    
    def clear(self) -> None:
        """Clear conversation history"""
        self.exchanges.clear()
    
    def get_all_exchanges(self) -> List[ConversationExchange]:
        """Get all conversation exchanges"""
        return self.exchanges.copy()
